package org.aceor.mddal.gateway.datatype.impl;

import com.google.common.math.IntMath;
import org.aceor.mddal.gateway.datatype.DataType;
import org.aceor.mddal.gateway.datatype.cal.AbstractCalculator;
import org.aceor.mddal.gateway.datatype.cal.Calculator;

import java.math.RoundingMode;

/**
 * Created by lxue on 16/4/16.
 */
public class IntegerType implements DataType<Integer> {

    private Calculator calculator = new AbstractCalculator() {
        @Override
        public Object doAdd(Object value1, Object value2) {
            return IntMath.checkedAdd((Integer)value1, (Integer)value2);
        }

        @Override
        public Object doSub(Object value1, Object value2) {
            return IntMath.checkedSubtract((Integer)value1, (Integer)value2);
        }

        @Override
        public Object doMultiply(Object value1, Object value2) {
            return IntMath.checkedMultiply((Integer)value1, (Integer)value2);
        }

        @Override
        public Object doDivide(Object value1, Object value2) {
            return IntMath.divide((Integer)value1, (Integer)value2, RoundingMode.DOWN);
        }
    };
    @Override
    public int compare(Integer o1, Integer o2) {
        int result = IntMath.checkedSubtract(o1, o2);
        if (result > 0) {
            return 1;
        } else if (result < 0) {
            return -1;
        } else {
            return 0;
        }
    }

    @Override
    public Calculator getCalculator() {
        return calculator;
    }
}
